home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / comm / misc / http_log_2.lha / httplog.c next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  11.5 KB  |  467 lines

  1. /*
  2. // httplog.c
  3. //
  4. // (c) Armin Obersteiner
  5. //
  6. // Parsing ncsa/cern httpd logfile
  7. //
  8. // USAGE: httplog [-s <keyword>] [-l] <logfile>
  9. // 
  10. // <logfile>      -  httpd <logfile>
  11. // -l             -  long: country statistics
  12. // -lt            -  long: time statistics
  13. // -s <keyword>   -  search for <keyword>
  14. //
  15. // compiles on:   MaxonC++ (amiga)  
  16. //                gcc (amiga)
  17. //                gcc (bsd)
  18. // (it should actually compile on any platform then :)
  19. //
  20. //  ->>>  gcc -o httplog httplog.c -lm
  21. //
  22. // it´s made to work on logfiles with american date format and austrian/german time format (24h)
  23. //
  24. //
  25. //    to include new countries:
  26. //
  27. //          -   add lines to structure "struct dummy c"
  28. //       -  don´t forget to increase "country_anz"
  29. //
  30. //    to adapt time and date for other logfile formats:
  31. //
  32. //          -   do the same with "struct dummy d" / "struct dummy t"
  33. //       -  don´t forget to increase/decrease "day_anz" :)  / "time_anz"
  34. //
  35. //    the second entry in these stuctures (dummy) is the string to search for
  36. //       (use spaces or brackets, because it´s more reliable then)
  37. //    the third entry is the string for output
  38. //
  39. //
  40. // CU Armin :)
  41. //
  42. //    ( Armin.Obersteiner@giga.or.at )
  43. */
  44.  
  45.  
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include <math.h>
  50.  
  51. FILE *f;
  52.  
  53. struct dummy
  54. {
  55.    int i;
  56.    char *ext;
  57.    char *name;
  58. };
  59.    
  60. int country_anz=17;
  61. struct dummy c[17]=
  62. {
  63.    {  0,".at ",   "austria          "  },
  64.    {  0,".de ",   "germany          "  },
  65.    {  0,".fi ",   "finnland         "  },
  66.    {  0,".se ",   "sweden           "  },
  67.    {  0,".no ",   "norway           "  },
  68.    {  0,".edu ",  "usa education    "  },
  69.    {  0,".com ",  "usa commercial   "  },
  70.    {  0,".gov ",  "usa goverment    "  },
  71.    {  0,".net ",  "network          "  },
  72.    {  0,".dk ",   "denmark          "  },
  73.    {  0,".uk ",   "great britain    "  },
  74.    {  0,".nl ",   "the netherlands  "  },
  75.    {  0,".nz ",   "new zeeland      "  },
  76.    {  0,".au ",   "australia        "  },
  77.    {  0,".gr ",   "greece           "  },
  78.    {  0,".fr ",   "france           "  },
  79.    {  0,".jp ",   "japan            "  }
  80. };
  81.  
  82. int day_anz=7;
  83. struct dummy d[7]=
  84. {
  85.    {  0,"Mon ",      "mon" },
  86.    {  0,"Tue ",      "tue" },
  87.    {  0,"Wed ",      "wed" },
  88.    {  0,"Thu ",      "thu" },
  89.    {  0,"Fri ",      "fri" },
  90.    {  0,"Sat ",      "sat" },
  91.    {  0,"Sun ",      "sun" },
  92. };
  93.  
  94. int time_anz=24;
  95. struct dummy t[24]=
  96. {
  97.    {  0," 00:",      "00"  },
  98.    {  0," 01:",      "01"  },
  99.    {  0," 02:",      "02"  },
  100.    {  0," 03:",      "03"  },
  101.    {  0," 04:",      "04"  },
  102.    {  0," 05:",      "05"  },
  103.    {  0," 06:",      "06"  },
  104.    {  0," 07:",      "07"  },
  105.    {  0," 08:",      "08"  },
  106.    {  0," 09:",      "09"  },
  107.    {  0," 10:",      "10"  },
  108.    {  0," 11:",      "11"  },
  109.    {  0," 12:",      "12"  },
  110.    {  0," 13:",      "13"  },
  111.    {  0," 14:",      "14"  },
  112.    {  0," 15:",      "15"  },
  113.    {  0," 16:",      "16"  },
  114.    {  0," 17:",      "17"  },
  115.    {  0," 18:",      "18"  },
  116.    {  0," 19:",      "19"  },
  117.    {  0," 20:",      "20"  },
  118.    {  0," 21:",      "21"  },
  119.    {  0," 22:",      "22"  },
  120.    {  0," 23:",      "23"  },
  121. };
  122.  
  123. char prg[256];
  124.  
  125. struct cache
  126. {
  127.     int year;
  128.     int value;
  129. };
  130.  
  131. struct cache ca={0,0};
  132.  
  133. void ende(int n,char *end);
  134. void parse_cern(char *d);
  135.  
  136. int date2day(int day,int month,int year);
  137. int sjahr(int year);
  138.  
  139.  
  140. void main(int argc, char *argv[])
  141. {
  142.    int anz=0,i=0,j=0,count=0,x;
  143.    double sonst=0;
  144.    int minus_l=0;
  145.    int minus_lt=0;
  146.    int minus_s=0;
  147.  
  148.    double prozent;
  149.  
  150.    char file[256];
  151.    char such[256];
  152.  
  153.    char line[1024],site[1024],back[1024],date[1024],back1[1024];
  154.    /* char method[1024],file[1024],protocol[1024]; */
  155.  
  156.    if(argc<2)
  157.    {
  158.       printf("USAGE: %s [-s <keyword>] [-l[t]] <logfile> \n",argv[0]);
  159.    }
  160.    else
  161.    {
  162.       strcpy(prg,argv[0]);
  163.    
  164.       if(!strcmp(argv[1],"-s"))
  165.       {
  166.          if(argc==4)
  167.          {
  168.             strcpy(such,argv[2]);
  169.             if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
  170.             if(!strcmp(argv[3],"-l") || !strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
  171.             strcpy(file,argv[3]);
  172.             minus_s=1;
  173.          }
  174.          else if(argc==5)
  175.          {
  176.             strcpy(such,argv[2]);
  177.             if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
  178.             if(strcmp(argv[3],"-l") && strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
  179.             strcpy(file,argv[4]);            
  180.             minus_s=1;
  181.             if(!strcmp(argv[3],"-l"))
  182.             {
  183.                minus_l=1;
  184.             }
  185.             else minus_lt=1;
  186.    
  187.          }
  188.          else ende(1,"wrong arguments");
  189.       }
  190.       else if( !strcmp(argv[1],"-l") )
  191.       {
  192.          if(argc!=3) ende(1,"wrong arguments");
  193.  
  194.          minus_l=1;
  195.          strcpy(file,argv[2]);
  196.       }
  197.       else if( !strcmp(argv[1],"-lt") )
  198.       {
  199.          if(argc!=3) ende(1,"wrong arguments");
  200.  
  201.          minus_lt=1;
  202.          strcpy(file,argv[2]);
  203.       }
  204.       else strcpy(file,argv[1]);
  205.  
  206.  
  207.       if( (f=fopen(file,"r"))!=NULL )
  208.       {
  209.          char site_old[1024]="";
  210.          char date_old[1024]="";
  211.  
  212.          int first=1;
  213.          int cern=0;
  214.  
  215.          while( ((fgets(line,1024,f))!=NULL) )
  216.             {
  217.                if(first)
  218.                {
  219.                   first=0;
  220.                   strcpy(back1,line);
  221.                   if(strstr(back1,"- -")!=NULL) cern=1;
  222.                }
  223.  
  224.                if(minus_s) strcpy(back,line);
  225.  
  226.                i++;
  227.                strcpy(site,strtok(line,"["));
  228.                strcpy(date,strtok(NULL,"]"));
  229.                /*strcpy(method,strtok(NULL," "));
  230.                strcpy(file,strtok(NULL," "));
  231.                strcpy(protocol,strtok(NULL," \0\n")); */
  232.  
  233.                if(minus_s)
  234.                {
  235.                   if(strstr(back,such)!=NULL)
  236.                   {
  237.                      if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
  238.                      if(minus_lt)
  239.                      {
  240.                            if(cern==0)
  241.                            {
  242.                               for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
  243.                               for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
  244.                            }
  245.                            else parse_cern(date);
  246.                      }
  247.                      anz++;
  248.                   }
  249.                }
  250.                else
  251.                {
  252.                   if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
  253.                   if(minus_lt)
  254.                   {
  255.                         if(cern==0)
  256.                         {
  257.                            for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
  258.                            for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
  259.                         }
  260.                         else parse_cern(date);
  261.                   }
  262.                   if(strcmp(site,site_old)!=0) anz++;
  263.                }
  264.  
  265.                if(!minus_s) strcpy(site_old,site);
  266.             }
  267.  
  268.          fclose(f);
  269.  
  270.          if(minus_s)
  271.          {
  272.             printf("searching for: %s\n\n",such);
  273.             printf("access: %d/%d  %8.4f %%\n",anz,i,(double)anz*(double)100/(double)i);
  274.          }
  275.          else printf("access: %d/%d\n",anz,i);
  276.          if(minus_l)
  277.          {
  278.             if(i==0) ende(2,"logfile wrong");
  279.  
  280.             printf("\n");
  281.             for(x=0;x<country_anz;x++) if( c[x].i )
  282.                {
  283.                   if(!minus_s)
  284.                   {
  285.                      prozent=(double)c[x].i*(double)100/(double)i;
  286.                   }
  287.                   else
  288.                   {
  289.                      prozent=(double)c[x].i*(double)100/(double)anz;
  290.                   }
  291.                   
  292.                   if(prozent>0.009) printf("%s: %6.2f %%\n",c[x].name,prozent);
  293.                   sonst=sonst+prozent;
  294.                }
  295.             prozent=(double)100-sonst;
  296.  
  297.             if(prozent>0.009) printf("\nothers           : %6.2f %%\n",prozent);
  298.          }
  299.          if(minus_lt)
  300.          {
  301.             if(i==0) ende(2,"logfile wrong");
  302.  
  303.             printf("\n");
  304.  
  305.             for(x=0;x<day_anz;x++) if( d[x].i )
  306.             {
  307.                if(!minus_s)
  308.                {
  309.                   prozent=(double)d[x].i*(double)100/(double)i;
  310.                }
  311.                else
  312.                {
  313.                   prozent=(double)d[x].i*(double)100/(double)anz;
  314.                }
  315.  
  316.                if(prozent>0.009) printf("%s: %6.2f %%\n",d[x].name,prozent);
  317.             }
  318.             printf("\n");
  319.             for(x=0;x<time_anz;x++) if( t[x].i )
  320.             {
  321.                if(!minus_s)
  322.                {
  323.                   prozent=(double)t[x].i*(double)100/(double)i;
  324.                }
  325.                else
  326.                {
  327.                   prozent=(double)t[x].i*(double)100/(double)anz;
  328.                }
  329.  
  330.                if(prozent>0.009) printf("%s: %6.2f %%\n",t[x].name,prozent);
  331.             }
  332.          }
  333.       }
  334.       else ende(5,"can´t open logfile");
  335.  
  336.       ende(0,"");
  337.    }
  338. }
  339.  
  340. void ende(int n,char *end)
  341. {
  342.    if(f) fclose(f);
  343.  
  344.    if(strcmp(end,"")!=0) printf("%s: %s !\n",&prg,end);
  345.    exit(n);
  346. }
  347.  
  348. void parse_cern(char *dd)
  349. {
  350.     char date[1024],hour[10];
  351.     char da[3],mo[4],yea[5];
  352.  
  353.     int h,x;
  354.     int day,mon,year;
  355.  
  356.     strcpy(date,strtok(dd,":"));
  357.     strcpy(hour,strtok(NULL,":"));
  358.  
  359.     h=atoi(hour);
  360.  
  361.    for(x=0;x<time_anz;x++) if(h==x) (t[x].i)++;
  362.  
  363.     strcpy(da,strtok(date,"/"));    
  364.     strcpy(mo,strtok(NULL,"/"));    
  365.     strcpy(yea,strtok(NULL,"\0"));
  366.     
  367.     day=atoi(da);
  368.  
  369.     if(!strcmp(mo,"Jan")) mon=1;
  370.     if(!strcmp(mo,"Feb")) mon=2;
  371.     if(!strcmp(mo,"Mar")) mon=3;
  372.     if(!strcmp(mo,"Apr")) mon=4;
  373.     if(!strcmp(mo,"May")) mon=5;
  374.     if(!strcmp(mo,"Jun")) mon=6;
  375.     if(!strcmp(mo,"Jul")) mon=7;
  376.     if(!strcmp(mo,"Aug")) mon=8;
  377.     if(!strcmp(mo,"Sep")) mon=9;
  378.     if(!strcmp(mo,"Oct")) mon=10;
  379.     if(!strcmp(mo,"Nov")) mon=11;
  380.     if(!strcmp(mo,"Dec")) mon=12;
  381.  
  382.     year=atoi(yea);
  383.  
  384.     h=date2day(day,mon,year);
  385.  
  386.     if(h!=0) for(x=0;x<day_anz;x++) if( h==(x+1) ) (d[x].i)++;
  387. }
  388.  
  389.  
  390. /*  date2day
  391.     (c) Armin.Obersteiner@giga.or.at
  392.  
  393.     input: <int> day,month,year
  394.     output:<int> 0,1-7
  395.  
  396.     0: error (before 1.1.1995 - this should be enough
  397.               if somebody knows a "day" before that please mail it to me)
  398.  
  399.     1-7: Monday-Sunday
  400.  
  401.     gcc: link with math lib !!
  402. */
  403.  
  404. int date2day(int day,int month,int year)
  405. {
  406.     int md[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  407.  
  408.     int j,i=0;
  409.     int jahr=0;
  410.     int s;
  411.  
  412.     s=sjahr(year);
  413.     if(ca.year==year)
  414.     {
  415.         i=ca.value;
  416.     }
  417.     else
  418.     {
  419.         for(j=1995;j<year;j++) if(sjahr(j)) i++;
  420.         ca.value=i;
  421.         ca.year=year;
  422.     }
  423.     jahr=(year-1995)*365+i+day;
  424.     for(i=1;i<month;i++) jahr=jahr+md[i-1];
  425.     if(s && (month>2)) jahr=jahr+1;
  426.  
  427.     switch((int)fmod((double)jahr,(double)7))
  428.         {
  429.             case 1: return 7;   /* 1.1.1995 is a Sunday ->7 */
  430.                     break;
  431.             case 2: return 1;
  432.                     break;
  433.             case 3: return 2;
  434.                     break;
  435.             case 4: return 3;
  436.                     break;
  437.             case 5: return 4;
  438.                     break;
  439.             case 6: return 5;
  440.                     break;
  441.             case 0: return 6;
  442.                     break;
  443.         }
  444. }
  445.  
  446. /*  sjahr
  447.     (c) Armin.Obersteiner@giga.or.at
  448.  
  449.     input:  <int> year
  450.     output: <int> 0,1
  451.  
  452.     0: normal year
  453.     1: leap-year
  454.  
  455.     gcc: link with math lib !!
  456. */
  457.  
  458. int sjahr(int year)
  459. {
  460.     int schalt=0;
  461.  
  462.     if(fmod((double)year,(double)4)==0) schalt=1;
  463.     if(fmod((double)year,(double)100)==0) if(fmod((double)year,(double)400)!=0) schalt=0;
  464.  
  465.     return schalt;
  466. }
  467.